package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：混元太极门
 * @date ：Created in 2022年06月08日
 * @description ：
 * @version: 1.0
 */
@Service
@Slf4j
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member xinxiupdate(Member member) {

    log.info("添加操作:",member);
        Member pojo = memberDao.selectPhoneNumber(member.getPhoneNumber());
        if (pojo!=null){
            member.setId(pojo.getId());//获取id  数据有没有
            memberDao.updateByIds(member);//更新操作

        } else {
            //添加
            member.setRegTime(new Date());//获取当前时间
            memberDao.tianjia(member);//没有就添加数据
        }
        return memberDao.selectById(member.getId());
    }

    /**
     * David Li
     *
     * @param months
     * @return
     */
    @Override
    public List<Integer> doCountByMonth(List<String> months) {
        //创建一个集合,来装查到的数据
        List<Integer> list = new ArrayList<>();
        //遍历集合参数
        for (String month : months) {
            //判断是第几个月
            String s = dayInMonth(month);
            String endDate = month+s;
            Long count = memberDao.countByRegTimeBefore(endDate);
            list.add(count.intValue());
        }
        return list;
    }
//定义一个方法,判断这个月有多少天
    private String dayInMonth(String month) {
        String[] split = month.split("\\.", 2);
        String s = split[1];
        int i = Integer.parseInt(s);
//        //定义一个月份为31天的数组
//        String[] day31 = new String[]{"1","3","5","7","8","10","12"};
//        String[] day30 = new String[]{"4","6","9","11"};
//        String[] day28 = new String[]{"2"};
        while (true){
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
                return ".31";
            }else if(i==4||i==6||i==9||i==11){
                return ".30";
            }else {
                return ".28";
            }
        }
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.selectById(id);
    }


}
